package sunstar.com.xq.activity

import android.graphics.Typeface
import android.os.Bundle
import android.support.design.widget.BottomSheetBehavior
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.support.v4.widget.NestedScrollView
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.animation.Animation
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.AnkoLogger
import sunstar.com.xq.R
import sunstar.com.xq.adapter.MyFragmentPageAdapter
import sunstar.com.xq.fragment.FriendFragment
import sunstar.com.xq.fragment.GeFragment
import sunstar.com.xq.fragment.MySelfFragment
import sunstar.com.xq.fragment.SquareFragment

class MainActivity : AppCompatActivity(),AnkoLogger {

    private var mainBaseFragment = SquareFragment()
    private var geFragment = GeFragment()
    private var friendFragment = FriendFragment()
    private var mySelfFragment = MySelfFragment()
    private var fragmentList = mutableListOf<Fragment>()
    private var pageAdapter : MyFragmentPageAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        JCVideoPlayer.ACTION_BAR_EXIST = false
        JCVideoPlayer.TOOL_BAR_EXIST = false
        setView()
    }
    private fun setView() {
        var typeFace = Typeface.createFromAsset(assets, "fonts/qingkai.ttf")
        bottom_sheet_text.setTypeface(typeFace)
        bottom_sheet_text_sure.setTypeface(typeFace)
        fragmentList.add(mainBaseFragment)
        fragmentList.add(geFragment)
        fragmentList.add(friendFragment)
        fragmentList.add(mySelfFragment)
        pageAdapter = MyFragmentPageAdapter(supportFragmentManager,fragmentList)
        main_activity_body.offscreenPageLimit = 4
        main_activity_body.adapter = pageAdapter
        mainBaseFragment.setScrollListener(object : NestedScrollView.OnScrollChangeListener{
            override fun onScrollChange(v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
                if(scrollY<oldScrollY){
                    if(BottomSheetBehavior.from(bottom_sheet).state == BottomSheetBehavior.STATE_HIDDEN){
                        BottomSheetBehavior.from(bottom_sheet).state =  BottomSheetBehavior.STATE_EXPANDED
                    }
                }else if(scrollY>oldScrollY){
                    if(BottomSheetBehavior.from(bottom_sheet).state == BottomSheetBehavior.STATE_EXPANDED){
                        BottomSheetBehavior.from(bottom_sheet).state =  BottomSheetBehavior.STATE_HIDDEN
                    }
                }
            }

        })
        main_activity_body.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{

            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                main_activity_title_text.changeText(position,positionOffset)

            }

            override fun onPageSelected(position: Int) {
                when(position){
                    0-> bottom_sheet_text.setText("题榜")
                    1-> bottom_sheet_text.setText("建盟")
                    2-> bottom_sheet_text.setText("寻友")
                    3-> bottom_sheet_text.setText("己身")
                }
            }
        })
        main_activity_title_text.setOnClickListener{
            var animit = main_activity_title_text.animate()
            animit.alpha(0f).duration = 2000
            animit.start()
            main_activity_title_text.visibility = View.GONE
            change_layout.alpha = 0f
            change_layout.visibility = View.VISIBLE
            var animit2 = change_layout.animate()
            animit2.alpha(1f).duration = 2000
            animit2.start()
        }
        change_bang.setOnClickListener{
            main_activity_body.setCurrentItem(0)
            changeRemove()
        }
        change_ge.setOnClickListener{
            main_activity_body.setCurrentItem(1)
            changeRemove()
        }
        change_you.setOnClickListener{
            main_activity_body.setCurrentItem(2)
            changeRemove()
        }
        change_jun.setOnClickListener{
            main_activity_body.setCurrentItem(3)
            changeRemove()
        }
    }

    private fun changeRemove() {
        var animit = change_layout.animate()
        animit.alpha(0f).duration = 2000
        animit.start()
        change_layout.visibility = View.GONE
        main_activity_title_text.alpha = 0f
        main_activity_title_text.visibility = View.VISIBLE
        var animit2 = main_activity_title_text.animate()
        animit2.alpha(1f).duration = 2000
        animit2.start()
    }
}
